package com.lagou.kafka.demo;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: TODO 消费者
 * @date 2022/6/11 14:41
 */
public class MyConsumer {


    public static void main(String[] args) {
        Map<String,Object> configs = new HashMap<>();
        configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.106.130:9092");
        configs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);
        // 很重要
        configs.put(ConsumerConfig.GROUP_ID_CONFIG,"mygrp1");
        KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(configs);
        // 订阅主题
        consumer.subscribe(Collections.singleton("tp_demo_01"));

        while(true){
            ConsumerRecords<String, String> records = consumer.poll(1000);
            records.forEach(stringStringConsumerRecord -> System.out.println(stringStringConsumerRecord));
        }
    }
}
